package com.tommy.foreground.dto;

import com.google.common.base.Converter;
import com.tommy.foreground.entity.Item;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;

/**
 * @Author: Tommy
 * @Description:
 * @Date: Created in 下午6:21 2017/11/27
 * @Modified By:
 */
@Accessors(chain = true)
@Data
public class ItemDTO {
    private Long id;

    private String title;

    private String sellPoint;

    public Item convertToItem() {
        ItemDTOConvert itemDTOConvert = new ItemDTOConvert();
        return itemDTOConvert.convert(this);
    }

    public ItemDTO convertFor(Item item) {
        ItemDTOConvert itemDTOConvert = new ItemDTOConvert();
        return itemDTOConvert.reverse().convert(item);
    }

    private static class ItemDTOConvert extends Converter<ItemDTO, Item> {
        @Override
        protected Item doForward(ItemDTO itemDTO) {
            Item item = new Item();
            BeanUtils.copyProperties(itemDTO, item);
            return item;
        }

        @Override
        protected ItemDTO doBackward(Item item) {
            ItemDTO itemDTO = new ItemDTO();
            BeanUtils.copyProperties(item, itemDTO);
            return itemDTO;
        }
    }
}
